import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.image.BufferStrategy;

public class GameWindow extends JFrame{

    private static int WINDOW_WIDTH  = 1000;
    private static int WINDOW_HEIGHT = 600;

    private int MENUHEIGHT = 40; //tant quil y a un problem de listener sur Display, il va falloir s'en remettre a ca, car le frame commence a listener des le menu, donc peut poser des problems sur le scroll

    private GameEngine     gameEngine;
    private Display        gameDisplay;
    private BufferStrategy strategy;
    /* VARIABLES LOCALES*/

    private final int UPDATE_RATE = 30; 

    /*camera top-left corner starting point*/
  private Camera cam;
    private double startX = 0;
    private double startY = 0;

    /* LIMITE POUR LE SCROLL */
    private int heightMargin = 15;
    private int widthMargin  = 15;

    /* vitesse de scroll */
    private double verticalScrollSpeed = 13;
    private double horizontalScrollSpeed = 13;
    
    private Point oldMousePos;
    private UpdateCycle gameThread;
    
  private int [][] deplacement;
  

  private Point ps = new Point(0,0);
  private Point pe = new Point(0,0);
  private boolean []click;
  
    public GameWindow(){
	super();

	setSize(WINDOW_WIDTH,WINDOW_HEIGHT);
	setResizable(false);
	setTitle("Age Of War");
	setLocationRelativeTo(null);
	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	setVisible(true);
	click = new boolean[2];
	click[0] = false;
	addMouseMotionListener(new Select(ps,pe,click));
	
	setIgnoreRepaint(true);
	createBufferStrategy(3);
	strategy = getBufferStrategy();	
	
	cam =  new Camera(new Point2D(startX,startY),new Dimension(WINDOW_WIDTH,WINDOW_HEIGHT));
	
	init_dep();
	int []point = {0,0};
	
	gameEngine  = new GameEngine(deplacement,point);
	gameDisplay = new Display(WINDOW_WIDTH,WINDOW_HEIGHT,
				  strategy,
				  cam,
				  point,
				  ps,
				  pe,
				  click);
	gameDisplay.setPreferredSize(new Dimension(WINDOW_WIDTH,WINDOW_HEIGHT));
	
	setContentPane(gameDisplay);
	
	


	gameThread = new UpdateCycle();
	gameThread.start();
	
	
    }

    public static void main(String []argv){
	GameWindow game = new GameWindow();
	
    }

    
  private void init_dep()
  {
    deplacement = new int[1000][2];
    int [] depart = {0,0};
    int [] arrivee = {360,0};

    double [] vec = new double[2];
    vec[0] = (arrivee[0] - depart[0])/1000.0;
    vec[1] = (arrivee[1] - depart[1])/1000.0;

    
    double []offset = {0,0};

    for (int i = 0; i < deplacement.length; i++)
      {	
	offset[0] += vec[0];
	offset[1] += vec[1];
	
	deplacement[i][0] = (int)offset[0]; 
	deplacement[i][1] = (int)offset[1]; 

      }

  }


    private void updateCameraPosition(long delta_time)
    {
	
	Point mousePosTmp = (this.getMousePosition() != null) ? this.getMousePosition() : oldMousePos;
	if (mousePosTmp != null){
	    
	    
	    double x = mousePosTmp.getX();
	    double y = mousePosTmp.getY();
	

	    
	    double scrollx = 0;
	    double scrolly = 0;
		
	    /*	    if ( (gameDisplay.getCamera().getWidth() - x) < widthMargin )
		scrollx = -horizontalScrollSpeed;
	    else
		if ( x < widthMargin )
		    scrollx = horizontalScrollSpeed;
	    
	    if ( (gameDisplay.getCamera().getHeight() - y) < heightMargin)
		scrolly = -verticalScrollSpeed;
	    else
		if ( y < heightMargin + MENUHEIGHT)
		scrolly = verticalScrollSpeed;*/

	    double coef =  delta_time / (double) UPDATE_RATE;
	   

	    if ( (cam.getWidth() - x) < widthMargin )
		scrollx = - horizontalScrollSpeed * coef;
	    else
		if ( x < widthMargin )
		    scrollx = horizontalScrollSpeed * coef;
	    
	    if ( (cam.getHeight() - y) < heightMargin)
		scrolly = - verticalScrollSpeed * coef;
	    else
		if ( y < heightMargin + MENUHEIGHT)
		scrolly = verticalScrollSpeed * coef;
	       
	    //	    gameDisplay.moveCamera(scrollx,scrolly);
	    cam.move(scrollx,scrolly);
	    oldMousePos = mousePosTmp;
	
	}

	    
    }
	
    class UpdateCycle extends Thread{
	
	public void run(){
	  long time_before = System.currentTimeMillis();
	  long time_after;
	  long delta_time;


	  while (true)
	    {
	      time_after = System.currentTimeMillis();
	      delta_time = time_after - time_before;
	      
	      
	      updateCameraPosition(delta_time);
	      gameEngine.update(delta_time);

	      gameDisplay.display();
	      
	      
	      time_before = time_after;
	      
	    }
	}
    }
}
   
